package com.sm.service;

import java.util.Date;
import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sm.model.SystemInformation;
import com.sm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.dao.SystemInformationMapper;

@Service
public class SystemInformationService {
	@Autowired
	private SystemInformationMapper systemInformationMapper;


    public int updateByPrimaryKeySelective(SystemInformation record) {
    	return systemInformationMapper.updateByPrimaryKeySelective(record);
    }

    public SystemInformation selectByPrimaryKey(Integer systemInformationId) {
    	return systemInformationMapper.selectByPrimaryKey(systemInformationId);
    }

    /**
     * 根据用户，类型，是否已读 查询消息个数
     *
     * @param userId 用户ID
     * @param type   类型 -1 全部消息 1我的消息 2系统消息
     * @param infoStatus 是否已读 -1 全部 0已读 1未读
     * @return
     */
    public int selectSysInfoCount(Integer userId, Integer type, Integer infoStatus) {
        return systemInformationMapper.selectSysInfoCount(userId, type,infoStatus);
    }

    /**
     * 添加系统消息通知记录
     * @param content   内容
     * @param title     标题
     * @param receiver  接受人ID
     * @return
     */
    public int insertSysInfo(String content,String title,Integer receiver){
        SystemInformation systemInformationNew = new SystemInformation();
        systemInformationNew.setSentTime(new Date());
        // 1我的消息 2系统消息
        systemInformationNew.setType(1);
        //0是已读，1是未读
        systemInformationNew.setInfoStatus(1);
        systemInformationNew.setInformationContent(content);
        systemInformationNew.setTitle(title);
        systemInformationNew.setReceiver(receiver);
        return systemInformationMapper.insertSelective(systemInformationNew);
    }

    /**
     * 获取我的系统消息列表
     * @param currentPage
     * @param pageSize
     * @param receiverId    接受人ID
     * @param type          类型 -1 全部消息 1我的消息 2系统消息
     * @return
     */
    public PageResult selectSysListByReceiver(Integer currentPage, Integer pageSize, Integer receiverId,Integer type) {
        PageHelper.startPage(currentPage, pageSize);
        Page<SystemInformation> systemInformations = systemInformationMapper.selectSysListByReceiver(receiverId,type);
        return new PageResult(systemInformations.getTotal(),systemInformations.getResult());
    }


    public void removeById(List<Integer> id) {
        systemInformationMapper.deleteByIds(id);
    }
}
